package edu.gatech.bsbmule.view;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import edu.gatech.bsbmule.model.Game;
import edu.gatech.bsbmule.model.Map;
import edu.gatech.bsbmule.model.Player;
import edu.gatech.bsbmule.model.Store;
import edu.gatech.bsbmule.presenter.GameConfigPresenter;
import edu.gatech.bsbmule.presenter.MapScreenPresenter;
import edu.gatech.bsbmule.presenter.PlayerConfigPresenter;
import edu.gatech.bsbmule.presenter.TownScreenPresenter;
import edu.gatech.bsbmule.presenter.WelcomeScreenPresenter;

/**
 * Driver of the whole game
 * 
 * @author BSB
 * @version 1.0 10/26/2013
 */
public class Main {

	public static void main(String args[]) {
		
		/**
		 * Construct the main window
		 */
		MainWindow main = MainWindow.getInstance();
		main.setSize(1200, 800);
		main.setPauseItemEnable(false);
		main.setSaveItemEnable(false);
		main.setVisible(true);
		
		/**
		 * Construct welcome screen and its presenter
		 */
		WelcomeScreen welcome = new WelcomeScreen();
		WelcomeScreenPresenter welcomePresenter
			= new WelcomeScreenPresenter(welcome);
		//Set main window to show welcome screen
		main.setPanel(welcome);

		/**
		 * Construct game configure screen, game, map and their presenters 
		 */
		//Set last presenter to display this screen
		GameConfig gameConfig = new GameConfig();
		welcomePresenter.next(gameConfig);
		welcomePresenter.setLoadButton(welcome);
		//models and presenters
		Game game = Game.getInstance();
		Map map = Map.getInstance();
		GameConfigPresenter gameConfigPresenter
			= new GameConfigPresenter(gameConfig);

		/**
		 * Construct player configure screen, players and its presenter
		 */
		//Make last presenter configure and set it to display this screen
		PlayerConfig playerConfig = new PlayerConfig();
		gameConfigPresenter.configureGameAndMap(game, map, playerConfig);
		//models and presenters
		List<Player> players = new ArrayList<Player>();
		PlayerConfigPresenter playerConfigPresenter
			= new PlayerConfigPresenter(playerConfig);

		/**
		 * Construct map screen and its presenter
		 */
		try {
			//view and presenter for map
			MapScreen mapScreen = MapScreen.getInstance();
			MapScreenPresenter mapScreenPresenter
				= new MapScreenPresenter(mapScreen);
			//view and presenter for town screen
			Store store = Store.getInstance(); 
			TownScreen townScreen = TownScreen.getInstance();
			TownScreenPresenter townScreenPresenter
				= new TownScreenPresenter(townScreen);
			townScreenPresenter.setLOButtonListener(mapScreenPresenter);
			//Set last presenter to display this screen
			playerConfigPresenter
				.configurePlayers(players, map, mapScreenPresenter, mapScreen);
		} catch (IOException e) {
			e.printStackTrace();
			System.err.println("Images not loaded!");
		}
	}
}
